define(['common/js/boss-frame-angular-dev-0.1'],function(){
	diretiveModule.directive('advanceTimeSelect',['$rootScope','MaiDialog',function($rootScope,MaiDialog){
		return {
			restrict:'AE',
			scope:{
				checkDo: '&afterCheckDo'
			},
			controller:'AdvanceTimeSelectController',
			templateUrl:"common/js/html/plusAdvanceTimeSelect.html",
			link:function(scope, iElement, iAttrs){

				scope.$on('init-CascadeTimeFrom',function(event,params){
					scope.init();
					scope.remainingDays = params.remainingDays;
					if(scope.remainingDays < 3){
						scope.selectAdvanceDays = -1;
						scope.spcAdvanceDays =  params.remainingDays==0?-1:params.remainingDays;
						scope.advanceDays = -1;

					}
				});
				scope.$on('validate-CascadeTimeFrom',function(){
					scope.check();
				})
			}
		};
	}]);

	controllerModule.controller("AdvanceTimeSelectController",["MaiDialog","$scope",function(MaiDialog,$scope){


		var reg = new RegExp("^[0-9]*$");
		$scope.init = function(){
			//预定时间
			$scope.advanceDays = 3;
			$scope.selectAdvanceDays = 3;
			$scope.flag = true;
			$scope.useDefineAdvanceDays = '';
			$scope.selectAdvanceDaysTips = ''
			//预订结束前天数	
			$scope.days = '';
			$scope.daysTip = '';
			$scope.dayFlag = true;
			//提醒周期
			$scope.alertInterval = '';
			$scope.alertIntervalTip = '';
		}


		//切换预订时间
		$scope.selectAdvance = function(){
			$scope.selectAdvanceDaysTips = ''
			if($scope.selectAdvanceDays == "-2"){
				$scope.flag = false;
				$scope.advanceDays = 0;
			}else{
				$scope.flag = true;
				$scope.useDefineAdvanceDays = '';
			}
			switch ($scope.selectAdvanceDays) {
				case "3":
					$scope.advanceDays = 3;
					break;
				case "7":
					$scope.advanceDays = 7;
					break;
				case "30":
					$scope.advanceDays = 30;
					break;
				case "-1":
					$scope.spcAdvanceDays = $scope.remainingDays==0?-1:$scope.remainingDays;
					$scope.advanceDays = -1;
					break;
				default:
					$scope.advanceDays = 0;
					break;
			}
		}

		$scope.check_A = function(){
			$scope.selectAdvanceDaysTips = ''
			try {
				if ($scope.useDefineAdvanceDays!=null&&$scope.useDefineAdvanceDays!=""
					&&reg.test($scope.useDefineAdvanceDays)&&$scope.useDefineAdvanceDays<=$scope.remainingDays) {
					$scope.advanceDays  = $scope.useDefineAdvanceDays;
				}else{
					if($scope.useDefineAdvanceDays==null||$scope.useDefineAdvanceDays==""){
						$scope.advanceDays = '';
					}else{
						$scope.advanceDays = $scope.remainingDays;
						$scope.useDefineAdvanceDays = $scope.remainingDays;
						$scope.selectAdvanceDaysTips = '【预订时间必须是正整数且不大于场次结束天数】';
					}

				}
			} catch (e) {
				$scope.useDefineAdvanceDays = $scope.remainingDays;
				$scope.selectAdvanceDaysTips = '【预订时间必须是正整数且不大于场次结束天数】';
			}
		}

		$scope.check_B = function(){
			$scope.daysTip = '';
			$scope.dayFlag = true;
			if($scope.days == ""||$scope.days==null){
				$scope.alertInterval = '';
				$scope.alertIntervalTip = '';
				return;
			}

			if(reg.test($scope.days)){
				if($scope.days == 0){
					$scope.alertInterval = '';
					$scope.daysTip = '【0天默认不触发预订提醒】';
				}else{
					if($scope.advanceDays !=-1 &&parseInt($scope.days)>parseInt($scope.advanceDays)){
						$scope.daysTip = '【不能大于预订时间天数】';
						$scope.days = $scope.advanceDays;
					}
					if($scope.advanceDays ==-1 &&parseInt($scope.days)>parseInt($scope.spcAdvanceDays)){
						$scope.daysTip = '【不能大于预订时间天数】';
						$scope.days = $scope.spcAdvanceDays;

					}

					$scope.dayFlag = false;
				}
			}else{
				$scope.days = '';
				$scope.daysTip = '【预订结束前天数必须是正整数】';
				$scope.alertInterval = '';
				$scope.alertIntervalTip = '';
				return;
			}
		}


		$scope.check_C = function(){
			$scope.alertIntervalTip = '';

			if($scope.alertInterval == null||$scope.alertInterval == ""){
				$scope.alertInterval = '';//默认一天提醒一次
				$scope.alertIntervalTip = '【输入间隔不能为空】';
			}else{
				if(reg.test($scope.alertInterval)&&parseInt($scope.alertInterval)<=parseInt($scope.days)){
				}else{
					$scope.alertInterval = 1;
					$scope.alertIntervalTip = '【输入间隔必须是正整数且不能大于预订提醒前天数】';
				}
			}
		}



		$scope.check=function(){
			$scope.alertIntervalTip ='';
			if($scope.advanceDays == ''||$scope.advanceDays == null||$scope.advanceDays == 0){
				$scope.selectAdvanceDaysTips = '【预订时间不能为空或者0天】';
				return;
			}

			if($scope.days!=null&&$scope.days!=""){
				if($scope.days == 0){
					console.info("0天默认不触发预订提醒");
				}else if(reg.test($scope.days)&&($scope.days<=$scope.advanceDays||$scope.days<=$scope.spcAdvanceDays)){
					if($scope.alertInterval == null||$scope.alertInterval == ""){
						$scope.alertIntervalTip = '【输入间隔不能为空】';
						return;
					}else{
						if(reg.test($scope.alertInterval)&&$scope.alertInterval<=$scope.days){
							if($scope.alertInterval == 0){
								$scope.alertIntervalTip = '【输入间隔不能为0】';
								return;
							}

						}else{
							$scope.alertIntervalTip = '【输入间隔不能大于预订提醒前天数】';
							return;
						}
					}
				}else{
					if($scope.alertInterval==''){
						$scope.alertIntervalTip="【输入间隔不能为空】";
						return;
					}

					$scope.daysTip = '【预订结束前天数必须是正整数且要小于等于预订时间】';
					return;
				}
			}
			var params = {"advanceDays":$scope.advanceDays ,"days":$scope.days,"alertInterval":$scope.alertInterval};
			$scope.checkDo({"params":params});
		}
		$scope.init();
	}]);




});